﻿using System.Collections.Generic;
using System.Linq;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class DefectsValidation : IValidator
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var entity in context.Metrics.UserStories)
            {
                if (entity.Bugs.Any() && context.Metrics.IsReadyForDevelopment(entity) && !context.Metrics.IsInProgress(entity))
                {
                    yield return new ValidationResult(
                        string.Format("{0}: {1} Is in dev or not started, but have bugs: {2}", entity.EntityType.Name, entity.Id, string.Join(", ", entity.Bugs.Select(x=>x.Id))))
                    {
                        Entity = entity
                    };
                }
                if (entity.Bugs.Count > entity.Tasks.Count )
                {
                    yield return new ValidationResult(
                        string.Format("{0}: {1} Too much bugs: {2}, for {3} tasks", entity.EntityType.Name, entity.Id, entity.Bugs.Count, entity.Tasks.Count), ValidationResultType.Warning)
                    {
                        Entity = entity
                    };
                }
            }
        }
    }
}
